package org.os.framework.context.model;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 拒绝策略
 * @author
 */
public enum RejectedExecutionHandlers {

    /**
     * 终止
     */
    Abort(ThreadPoolExecutor.AbortPolicy.class),

    /**
     * 丢弃
     */
    Discard(ThreadPoolExecutor.DiscardPolicy.class),

    /**
     * 丢弃旧的
     */
    DiscardOldest(ThreadPoolExecutor.DiscardOldestPolicy.class),

    /**
     * 运行新的
     */
    CallerRuns(ThreadPoolExecutor.CallerRunsPolicy.class);

    private Class<? extends RejectedExecutionHandler> clazz;

    private RejectedExecutionHandlers(Class<? extends RejectedExecutionHandler> clazz) {
        this.clazz = clazz;
    }

    public RejectedExecutionHandler getRejectedExecutionHandler() {
        try {
            return this.clazz.newInstance();
        } catch (Exception e) {
            return new ThreadPoolExecutor.AbortPolicy();
        }
    }

}
